define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {field: 'ROW_ID',title: '序号',width: 60, fixed: 'left'}
            , {field: 'NAME',title: '人员',width: 150, fixed: 'left',sort:true}
            , {field: 'finish',title: '成交家数',width: 80,sort:true,search:'default'}
            , {field: 'COUNT',title: '拜访家数',width: 80,sort:true,search:'default'}
            , {field: 'percent',title: '成功率',width: 80,filterDatas: app.datas_('sys_y_n'),sort:true},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai740setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai740SettingAction,
        moduleName = '拜访成功率'
    ;

    app.newModule({
        init: function () {
            self = this;
            let _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai740',
                _searchs: [{
                    id: 'gwai740', tableId: 'bfcgl',
                    datas:  _searchsArr.pushArray([
                        { field: 'GWAKL003', width: 160, title: '客户',sort: true,
                            search: 'default', searchOpts:{icon:true}}
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwai740_edit', tpl: 'gwai740_edit'
                },{
                    dom: 'gwai740_shouli', tpl: 'gwai740_shouli'
                },{
                    dom: 'gwai740_paigong', tpl: 'gwai740_paigong'
                },{
                    dom: 'gwai740_huifang', tpl: 'gwai740_huifang'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);

            self.data={
                list:[],
                names:[],
                percents:[],
                startDate:'',
                endDate:'',
                dateRange:''
            };
        },

        initTable: function (data) {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'bfcgl'),
                    title: moduleName + '信息表',
                    id: 'bfcgl',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    data:self.data.list,
                    loading: true,
                    page: true,
                    where: table_where
                });

                layui.laydate.render({
                    elem: '#test6'
                    ,range: true
                });

            });

        },
        //初始化数据
        initData: function () {
            app.ajax({
                xurl: "/bb/bfcgl/getInfo",
                xdata: {
                    startDate: self.data.startDate,
                    endDate: self.data.endDate
                },
                loading:true,
                cb: function (result) {

                    var data = result.data.data;
                    data.sortByKey('percent', true);
                    data.forEach(function (item, index) {
                        item.ROW_ID = index+1;
                    })
                    self.data.list = data;
                    self.formChat(data);
                    self.initTable(data);
                }
            });
        },
        formChat: function (data) {

            for (var item of data) {
                self.data.names.push(item.NAME);
                self.data.percents.push(item.percent)
            }
            var chartDiv = echarts.init(document.getElementById('divChart'));

            var getOption = function () {
                var chartOption = {
                    legend: {
                        data: self.data.names
                    },
                    grid: {
                        x: 35,
                        x2: 10,
                        y: 30,
                        y2: 25
                    },
                    // 提示框组件
                    tooltip: {
                        trigger: 'axis', // 触发类型。坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用/none什么都不触发
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            mark: {
                                show: true
                            },
                            dataView: {
                                show: true,
                                readOnly: false
                            },
                            magicType: {
                                show: true,
                                type: ['line']
                            },
                            restore: {
                                show: true
                            },
                            saveAsImage: {
                                show: true
                            }
                        }
                    },
                    calculable: false,
                    xAxis: [{
                        type: 'category',
                        data: self.data.names
                    }],
                    yAxis: [{
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}%'
                        },
                        splitArea: {
                            show: true
                        }
                    }],
                    series: [{
                        name: '百分比',
                        type: 'line',
                        data: self.data.percents,
                        label: {
                            normal: {
                                formatter: function (a) {
                                    return a.data + '%';
                                },
                                show: true,
                            }
                        },
                    }]
                };
                return chartOption;
            };
            chartDiv.setOption(getOption());
            self.data.names.pushArray([],true);
            self.data.percents.pushArray([],true);
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai740_table', where: table_where});
        },
        _ready: function () {
            this._super({
            });

            var month = new Date().getCurrentMonth();
            self.data.startDate = month[0];//本月开始日期
            self.data.endDate = new Date().Format('yyyy-MM-dd');

            new Vue({
                el:document.getElementById('page'),
                data:{
                    dateRange:[self.data.startDate,self.data.endDate]
                },
                watch:{
                  'dateRange':function (res) {
                    var startDate=  new Date(res[0]);
                    var startmonth=startDate.getMonth()+1;
                    var startday=startDate.getDate();
                    var endDate=  new Date(res[1]);
                    var endmonth=endDate.getMonth()+1;
                    var endday=endDate.getDate();
                    self.data.startDate =startDate.getFullYear()+'-'+(startmonth<10?'0'+startmonth:startmonth)+'-'+(startday<10?'0'+startday:startday);
                    self.data.endDate = endDate.getFullYear()+'-'+(endmonth<10?'0'+endmonth:endmonth)+'-'+(endday<10?'0'+endday:endday);
                    self.initData();
                  }
                },
                methods:{}

            })
            self.initData();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});